import dayjs from "dayjs";
import i18n from "@/lang";
const $t = i18n.t.bind(i18n);
export default {
    /* =============== 时间 ================ */
    /**
     * 将秒时间戳或毫秒时间戳转换成对应格式
     * @param {*} timestamp 秒时间戳或毫秒时间戳
     * @param {Number} type 预设时间格式对应的数字
     */
    timeFilter(value, type = 1) {
        if (value == null) return "-";
        if (value.toString() === "0") return "-";
        switch (type) {
            case 1:
                return dayjs.unix(value).format("YYYY-MM-DD HH:mm:ss");
            case 2:
                return dayjs.unix(value).format("YYYY-MM-DD HH:mm");
            case 3:
                return dayjs.unix(value).format("YYYY-MM-DD");
            case 4:
                return dayjs.unix(value).format("YYYY年MM月DD日");
            case 5:
                return dayjs.unix(value).format("HH:mm:ss");
            case 6:
                return dayjs.unix(value).format("MM.DD.YYYY");
            default:
                return "-";
        }
    },
    /**
     * 将秒时间戳转换成对应格式
     * @param {*} timestamp 秒时间戳
     * @param {String} format 时间格式
     */
    timeFormat(timestamp, format = "YYYY-MM-DD HH:mm:ss") {
        if (timestamp == null) return "";
        if (timestamp.toString() === "0") return "-";
        return dayjs.unix(timestamp).format(format);
    },
    /**
    //  判断时间间隔 几个小时前 几天前 几个月前 几年前
     * @param {String} time 时间戳
     * @typeBoolean { Boolean } 七天以上按日期显示
     * */ 
    timeAgo(time,typeBoolean = false) {
        if (!time) return "";
        let now = new Date().getTime();
        let oldTime = new Date(time).getTime();
        let difference = now - oldTime;
        let result = "";
        let minute = 1000 * 60;
        let hour = minute * 60;
        let day = hour * 24;
        let month = day * 30;
        let year = month * 12;
        let _year = difference / year;
        let _month = difference / month;
        let _week = difference / (7 * day);
        let _day = difference / day;
        let _hour = difference / hour;
        let _min = difference / minute;
        if(_day >= 7 && typeBoolean){
            result = this.timeFilter(oldTime/1000 , 6)
        }else if (_year >= 1) {
            result = ~~_year + $t("time.yearAgo");
        } else if (_month >= 1) {
            result = ~~_month + $t("time.monthAgo");
        } else if (_week >= 1) {
            result = ~~_week + $t("time.weekAgo");
        } else if (_day >= 1) {
            result = ~~_day +  $t("time.dayAgo");
        } else if (_hour >= 1) {
            result = ~~_hour + $t("time.hourAgo");
        } else if (_min >= 1) {
            result = ~~_min + $t("time.minuteAgo");
        } else {
            result = $t("time.justNow");
        }
        return result;
    },

    /* =============== 时间 ================ */
};
